/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.view;

import javax.swing.JOptionPane;
import org.colswe.solestac.control.LoginControl;
import org.colswe.solestac.entity.ActualUser;
/**
 *
 * @author Julian Pena
 */
public class Login extends javax.swing.JPanel {

    /**
     * Creates new form Login
     */
    public Login() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        usuarioTF = new javax.swing.JTextField();
        contrasenaTF = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        aceptarB = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Bitstream Vera Sans", 1, 16)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel1.setText("usuario");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(40, 70, 0, 0);
        add(jLabel1, gridBagConstraints);

        usuarioTF.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        usuarioTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                usuarioTFKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 101;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(37, 33, 0, 0);
        add(usuarioTF, gridBagConstraints);

        contrasenaTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                contrasenaTFKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 101;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 33, 0, 0);
        add(contrasenaTF, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Bitstream Vera Sans", 1, 16)); // NOI18N
        jLabel2.setText("Contraseña");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(21, 70, 0, 0);
        add(jLabel2, gridBagConstraints);

        aceptarB.setFont(new java.awt.Font("Bitstream Vera Sans", 1, 16)); // NOI18N
        aceptarB.setText("Aceptar");
        aceptarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarBActionPerformed(evt);
            }
        });
        aceptarB.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                aceptarBKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 63, 0, 0);
        add(aceptarB, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void usuarioTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usuarioTFKeyPressed
        if (evt.getKeyChar() == '\n') {
            contrasenaTF.requestFocus();
        }
    }//GEN-LAST:event_usuarioTFKeyPressed

    private void contrasenaTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_contrasenaTFKeyPressed
        if (evt.getKeyChar() == '\n') {
            this.aceptarBActionPerformed(null);
        }
    }//GEN-LAST:event_contrasenaTFKeyPressed

    private void aceptarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarBActionPerformed
        String usuario = usuarioTF.getText();
        String contrasena = String.valueOf(contrasenaTF.getPassword());
        try{
            LoginControl lc = new LoginControl();
            boolean existe = lc.login(usuario, contrasena);
            if(!existe){
                showWarningMssg("Combinación usuario contrasena incorrecta");
            }
            else{                
                ActualUser au = ActualUser.getInstance();                
                Principal principal = au.getPrincipal();                
                principal.welcome();
            }
            
        }catch(Exception e){
             showErrorMssg("Ha ocurrido un error");            
        }
    }//GEN-LAST:event_aceptarBActionPerformed

    private void showWarningMssg(final String Mssg){
        JOptionPane.showMessageDialog(this, Mssg, "Advertencia",JOptionPane.WARNING_MESSAGE);
    }
    
    private void showErrorMssg(final String Mssg){
        JOptionPane.showMessageDialog(this, Mssg, "Error", JOptionPane.ERROR_MESSAGE);
    }
    
     private void showConfirmMssg(final String Mssg){
        JOptionPane.showMessageDialog(this, Mssg);
    }
    
    private void aceptarBKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_aceptarBKeyPressed
        if (evt.getKeyChar() == '\n') {
            this.aceptarBActionPerformed(null);
        }
    }//GEN-LAST:event_aceptarBKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptarB;
    private javax.swing.JPasswordField contrasenaTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField usuarioTF;
    // End of variables declaration//GEN-END:variables
}
